clear all
set more off

*add your path here:
cd "C:\Users\jamesCabral\OneDrive - DAZ\CGH_Empirical_Replication"

******************************************************************

cd "data\ATUS_AHTUS_raw_data"

foreach year of numlist 1993 1995 1998{
	clear	

	quietly infix                    ///
	  byte    rectype       1-1      ///
	  int     sample        2-5      ///
	  double  ident         6-14     ///
	  int     year          15-18    ///
	  byte    age           19-20    ///
	  byte    sex           21-22    ///
	  byte    educ          23-24    ///
	  double  recwght       25-40    ///
	  byte    ethnic2       41-42    ///
	  double  infltwt       43-58    ///
	  double  xtimewt       59-74    ///
	  byte    state         75-76    ///
	  byte    under18       77-78    ///
	  int     act_chcare    79-82    ///
	  int     act_civic     83-86    ///
	  int     act_educa     87-90    ///
	  int     act_inhome    91-94    ///
	  int     act_media     95-98    ///
	  int     act_missing   99-102   ///
	  int     act_outhome   103-106  ///
	  int     act_pcare     107-110  ///
	  int     act_physical  111-114  ///
	  int     act_travel    115-118  ///
	  int     act_undom     119-122  ///
	  int     act_work      123-126  ///
	  using "ahtus_`year'.dat"

	gen  _line_num = _n
	drop if rectype != 2
	sort _line_num
	save __temp_ipums_hier_2.dta

	clear
	quietly infix                    ///
	  byte    rectype       1-1      ///
	  int     sample        2-5      ///
	  double  ident         6-14     ///
	  int     year          15-18    ///
	  byte    main          19-20    ///
	  int     time          21-24    ///
	  using "ahtus_`year'.dat"
	gen  _line_num = _n
	drop if rectype != 3
	sort _line_num
	save __temp_ipums_hier_3.dta

	clear
	use __temp_ipums_hier_2.dta
	append using __temp_ipums_hier_3.dta
	sort _line_num
	drop _line_num
	erase __temp_ipums_hier_2.dta
	erase __temp_ipums_hier_3.dta

	format ident        %9.0f
	format recwght      %16.0g
	format infltwt      %16.0g
	format xtimewt      %16.0g
	format ident        %9.0f

	label var rectype      `"Record Type (Person)"'
	label var sample       `"Sample"'
	label var ident        `"Identifier"'
	label var year         `"Year diary kept"'
	label var age          `"Age"'
	label var sex          `"Sex"'
	label var educ         `"Education"'
	label var recwght      `"Recommended sample (day) weight removing low quality diaries and missing age or "'
	label var ethnic2      `"Ethnic group (general)"'
	label var infltwt      `"RECWGHT inflated to national population"'
	label var xtimewt      `"RECWGHT limited to states in all samples"'
	label var state        `"State"'
	label var under18      `"Number of children under 18 in the household"'
	label var main         `"Main activity"'
	label var time         `"Duration of the activity"'
	label var act_chcare   `"ACT: Child care"'
	label var act_civic    `"ACT: Adult care, civic, voluntary, and religious activities"'
	label var act_educa    `"ACT: Education"'
	label var act_inhome   `"ACT: In home free time leisure"'
	label var act_media    `"ACT: Media and computing"'
	label var act_missing  `"ACT: Missing Activities"'
	label var act_outhome  `"ACT: Out of home free time and leisure"'
	label var act_pcare    `"ACT: Personal care"'
	label var act_physical `"ACT: Sports, exercise, and outdoor activities"'
	label var act_travel   `"ACT: Travel"'
	label var act_undom    `"ACT: Unpaid domestic work"'
	label var act_work     `"ACT: Paid Work"'

	label define rectype_lbl 2 `"Person"'
	label define rectype_lbl 3 `"Activity"', add
	label values rectype rectype_lbl

	label define year_lbl 1930 `"1930"'
	label define year_lbl 1931 `"1931"', add
	label define year_lbl 1965 `"1965"', add
	label define year_lbl 1966 `"1966"', add
	label define year_lbl 1975 `"1975"', add
	label define year_lbl 1976 `"1976"', add
	label define year_lbl 1985 `"1985"', add
	label define year_lbl 1992 `"1992"', add
	label define year_lbl 1993 `"1993"', add
	label define year_lbl 1994 `"1994"', add
	label define year_lbl 1995 `"1995"', add
	label define year_lbl 1998 `"1998"', add
	label define year_lbl 1999 `"1999"', add
	label define year_lbl 2000 `"2000"', add
	label define year_lbl 2003 `"2003"', add
	label define year_lbl 2004 `"2004"', add
	label define year_lbl 2005 `"2005"', add
	label define year_lbl 2006 `"2006"', add
	label define year_lbl 2007 `"2007"', add
	label define year_lbl 2008 `"2008"', add
	label define year_lbl 2009 `"2009"', add
	label define year_lbl 2010 `"2010"', add
	label define year_lbl 2011 `"2011"', add
	label define year_lbl 2012 `"2012"', add
	label define year_lbl 2018 `"2018"', add
	label values year year_lbl

	label define age_lbl -8 `"Missing or dirty on the case record"'
	label define age_lbl 00 `"0"', add
	label define age_lbl 01 `"1"', add
	label define age_lbl 02 `"2"', add
	label define age_lbl 03 `"3"', add
	label define age_lbl 04 `"4"', add
	label define age_lbl 05 `"5"', add
	label define age_lbl 06 `"6"', add
	label define age_lbl 07 `"7"', add
	label define age_lbl 08 `"8"', add
	label define age_lbl 09 `"9"', add
	label define age_lbl 10 `"10"', add
	label define age_lbl 11 `"11"', add
	label define age_lbl 12 `"12"', add
	label define age_lbl 13 `"13"', add
	label define age_lbl 14 `"14"', add
	label define age_lbl 15 `"15"', add
	label define age_lbl 16 `"16"', add
	label define age_lbl 17 `"17"', add
	label define age_lbl 18 `"18"', add
	label define age_lbl 19 `"19"', add
	label define age_lbl 20 `"20"', add
	label define age_lbl 21 `"21"', add
	label define age_lbl 22 `"22"', add
	label define age_lbl 23 `"23"', add
	label define age_lbl 24 `"24"', add
	label define age_lbl 25 `"25"', add
	label define age_lbl 26 `"26"', add
	label define age_lbl 27 `"27"', add
	label define age_lbl 28 `"28"', add
	label define age_lbl 29 `"29"', add
	label define age_lbl 30 `"30"', add
	label define age_lbl 31 `"31"', add
	label define age_lbl 32 `"32"', add
	label define age_lbl 33 `"33"', add
	label define age_lbl 34 `"34"', add
	label define age_lbl 35 `"35"', add
	label define age_lbl 36 `"36"', add
	label define age_lbl 37 `"37"', add
	label define age_lbl 38 `"38"', add
	label define age_lbl 39 `"39"', add
	label define age_lbl 40 `"40"', add
	label define age_lbl 41 `"41"', add
	label define age_lbl 42 `"42"', add
	label define age_lbl 43 `"43"', add
	label define age_lbl 44 `"44"', add
	label define age_lbl 45 `"45"', add
	label define age_lbl 46 `"46"', add
	label define age_lbl 47 `"47"', add
	label define age_lbl 48 `"48"', add
	label define age_lbl 49 `"49"', add
	label define age_lbl 50 `"50"', add
	label define age_lbl 51 `"51"', add
	label define age_lbl 52 `"52"', add
	label define age_lbl 53 `"53"', add
	label define age_lbl 54 `"54"', add
	label define age_lbl 55 `"55"', add
	label define age_lbl 56 `"56"', add
	label define age_lbl 57 `"57"', add
	label define age_lbl 58 `"58"', add
	label define age_lbl 59 `"59"', add
	label define age_lbl 60 `"60"', add
	label define age_lbl 61 `"61"', add
	label define age_lbl 62 `"62"', add
	label define age_lbl 63 `"63"', add
	label define age_lbl 64 `"64"', add
	label define age_lbl 65 `"65"', add
	label define age_lbl 66 `"66"', add
	label define age_lbl 67 `"67"', add
	label define age_lbl 68 `"68"', add
	label define age_lbl 69 `"69"', add
	label define age_lbl 70 `"70"', add
	label define age_lbl 71 `"71"', add
	label define age_lbl 72 `"72"', add
	label define age_lbl 73 `"73"', add
	label define age_lbl 74 `"74"', add
	label define age_lbl 75 `"75"', add
	label define age_lbl 76 `"76"', add
	label define age_lbl 77 `"77"', add
	label define age_lbl 78 `"78"', add
	label define age_lbl 79 `"79"', add
	label define age_lbl 80 `"80"', add
	label define age_lbl 81 `"81"', add
	label define age_lbl 82 `"82"', add
	label define age_lbl 83 `"83"', add
	label define age_lbl 84 `"84"', add
	label define age_lbl 85 `"85"', add
	label define age_lbl 86 `"86"', add
	label define age_lbl 87 `"87"', add
	label define age_lbl 88 `"88"', add
	label define age_lbl 89 `"89"', add
	label define age_lbl 90 `"90"', add
	label define age_lbl 91 `"91"', add
	label define age_lbl 92 `"92"', add
	label define age_lbl 93 `"93"', add
	label define age_lbl 94 `"94"', add
	label values age age_lbl

	label define sex_lbl -8 `"Missing or dirty on case record"'
	label define sex_lbl 01 `"Male"', add
	label define sex_lbl 02 `"Female"', add
	label values sex sex_lbl

	label define educ_lbl -8 `"Missing or dirty on the case record"'
	label define educ_lbl 01 `"0 - 8th Grade"', add
	label define educ_lbl 02 `"9 - 11th Grade"', add
	label define educ_lbl 03 `"High School Graduate"', add
	label define educ_lbl 04 `"Some College"', add
	label define educ_lbl 05 `"College Graduate"', add
	label define educ_lbl 06 `"Post College"', add
	label define educ_lbl 09 `"NIU"', add
	label values educ educ_lbl

	label define ethnic2_lbl -8 `"Missing or dirty on case record"'
	label define ethnic2_lbl 01 `"White"', add
	label define ethnic2_lbl 02 `"Black"', add
	label define ethnic2_lbl 03 `"Some other race"', add
	label define ethnic2_lbl 09 `"NIU"', add
	label values ethnic2 ethnic2_lbl

	label define state_lbl -9 `"Not Available"'
	label define state_lbl -8 `"Missing or dirty on the case record"', add
	label define state_lbl 01 `"Alabama"', add
	label define state_lbl 02 `"Alaska"', add
	label define state_lbl 03 `"Arizona"', add
	label define state_lbl 04 `"Arkansas"', add
	label define state_lbl 05 `"California"', add
	label define state_lbl 06 `"Colorado"', add
	label define state_lbl 07 `"Connecticut"', add
	label define state_lbl 08 `"Delaware"', add
	label define state_lbl 09 `"District of Columbia"', add
	label define state_lbl 10 `"Florida"', add
	label define state_lbl 11 `"Georgia"', add
	label define state_lbl 12 `"Hawaii"', add
	label define state_lbl 13 `"Idaho"', add
	label define state_lbl 14 `"Illinois"', add
	label define state_lbl 15 `"Indiana"', add
	label define state_lbl 16 `"Iowa"', add
	label define state_lbl 17 `"Kansas"', add
	label define state_lbl 18 `"Kentucky"', add
	label define state_lbl 19 `"Louisiana"', add
	label define state_lbl 20 `"Maine"', add
	label define state_lbl 21 `"Maryland"', add
	label define state_lbl 22 `"Massachusetts"', add
	label define state_lbl 23 `"Michigan"', add
	label define state_lbl 24 `"Minnesota"', add
	label define state_lbl 25 `"Mississippi"', add
	label define state_lbl 26 `"Missouri"', add
	label define state_lbl 27 `"Montana"', add
	label define state_lbl 28 `"Nebraska"', add
	label define state_lbl 29 `"Nevada"', add
	label define state_lbl 30 `"New Hampshire"', add
	label define state_lbl 31 `"New Jersey"', add
	label define state_lbl 32 `"New Mexico"', add
	label define state_lbl 33 `"New York"', add
	label define state_lbl 34 `"North carolina"', add
	label define state_lbl 35 `"North Dakota"', add
	label define state_lbl 36 `"Ohio"', add
	label define state_lbl 37 `"Oklahoma"', add
	label define state_lbl 38 `"Oregon"', add
	label define state_lbl 39 `"Pennsylvania"', add
	label define state_lbl 40 `"Rhode Island"', add
	label define state_lbl 41 `"South Carolina"', add
	label define state_lbl 42 `"South Dakota"', add
	label define state_lbl 43 `"Tennessee"', add
	label define state_lbl 44 `"Texas"', add
	label define state_lbl 45 `"Utah"', add
	label define state_lbl 46 `"Vermont"', add
	label define state_lbl 47 `"Virginia"', add
	label define state_lbl 48 `"Washington"', add
	label define state_lbl 49 `"West Virginia"', add
	label define state_lbl 50 `"Wisconsin"', add
	label define state_lbl 51 `"Wyoming"', add
	label values state state_lbl

	label define under18_lbl -8 `"Missing or dirty on case record"'
	label define under18_lbl 00 `"0"', add
	label define under18_lbl 01 `"1"', add
	label define under18_lbl 02 `"2"', add
	label define under18_lbl 03 `"3"', add
	label define under18_lbl 04 `"4"', add
	label define under18_lbl 05 `"5"', add
	label define under18_lbl 06 `"6"', add
	label define under18_lbl 07 `"7"', add
	label define under18_lbl 08 `"8"', add
	label define under18_lbl 09 `"9"', add
	label define under18_lbl 10 `"10"', add
	label define under18_lbl 11 `"More than 10"', add
	label values under18 under18_lbl

	label define main_lbl 01 `"General or other personal care"'
	label define main_lbl 02 `"Imputed personal or household care"', add
	label define main_lbl 03 `"Sleep"', add
	label define main_lbl 04 `"Imputed sleep"', add
	label define main_lbl 05 `"Naps and rest"', add
	label define main_lbl 06 `"Wash, dress, personal care"', add
	label define main_lbl 07 `"Personal medical care"', add
	label define main_lbl 08 `"Meals at work"', add
	label define main_lbl 09 `"Other meals and snacks"', add
	label define main_lbl 10 `"Main paid work (not at home)"', add
	label define main_lbl 11 `"Paid work at home"', add
	label define main_lbl 12 `"Second job, other paid work"', add
	label define main_lbl 13 `"Work breaks"', add
	label define main_lbl 14 `"Other time at workplace"', add
	label define main_lbl 15 `"Time looking for work"', add
	label define main_lbl 16 `"Regular schooling, education"', add
	label define main_lbl 17 `"Homework"', add
	label define main_lbl 18 `"Short course or training"', add
	label define main_lbl 19 `"Occasional or other education/training"', add
	label define main_lbl 20 `"Food preparation, cooking"', add
	label define main_lbl 21 `"Set table, wash/put away dishes"', add
	label define main_lbl 22 `"Cleaning"', add
	label define main_lbl 23 `"Laundry, ironing, clothing repair"', add
	label define main_lbl 24 `"Home repairs, maintain vehicle"', add
	label define main_lbl 25 `"Other domestic work"', add
	label define main_lbl 26 `"Purchase routine goods"', add
	label define main_lbl 27 `"Purchase consumer durables"', add
	label define main_lbl 28 `"Purchase personal services"', add
	label define main_lbl 29 `"Purchase medical services"', add
	label define main_lbl 30 `"Purchase repair, laundry services"', add
	label define main_lbl 31 `"Financial/government services"', add
	label define main_lbl 32 `"Purchase other services"', add
	label define main_lbl 33 `"Care of infants"', add
	label define main_lbl 34 `"General care of older children"', add
	label define main_lbl 35 `"Medical care of children"', add
	label define main_lbl 36 `"Play with children"', add
	label define main_lbl 37 `"Supervise child or help with homework"', add
	label define main_lbl 38 `"Read to, talk with child"', add
	label define main_lbl 39 `"Other child care"', add
	label define main_lbl 40 `"Adult care"', add
	label define main_lbl 41 `"General voluntary acts"', add
	label define main_lbl 42 `"Political and civic activity"', add
	label define main_lbl 43 `"Union and professional activities"', add
	label define main_lbl 44 `"Volunteer child/family organization"', add
	label define main_lbl 45 `"Volunteer fraternal organization"', add
	label define main_lbl 46 `"Other formal volunteering"', add
	label define main_lbl 48 `"Acts for religious organization"', add
	label define main_lbl 49 `"Worship and religious acts"', add
	label define main_lbl 50 `"General out-of-home leisure"', add
	label define main_lbl 51 `"Attend sporting event"', add
	label define main_lbl 52 `"Go to cinema"', add
	label define main_lbl 53 `"Theater, concert, opera"', add
	label define main_lbl 54 `"Museums, exhibitions"', add
	label define main_lbl 55 `"Attend other public event"', add
	label define main_lbl 56 `"Restaurant, cafe bar"', add
	label define main_lbl 57 `"Parties or receptions"', add
	label define main_lbl 58 `"Imputed time away from home"', add
	label define main_lbl 60 `"Sports and exercise"', add
	label define main_lbl 62 `"Walking"', add
	label define main_lbl 63 `"Cycling"', add
	label define main_lbl 64 `"Outdoor recreation"', add
	label define main_lbl 65 `"Physical activity, sports with child"', add
	label define main_lbl 66 `"Hunting, fishing, boating, hiking"', add
	label define main_lbl 67 `"Gardening"', add
	label define main_lbl 68 `"Pet care, walk dogs"', add
	label define main_lbl 70 `"General indoor leisure"', add
	label define main_lbl 71 `"Imputed in-home social"', add
	label define main_lbl 72 `"Receive or visit friends"', add
	label define main_lbl 73 `"Other in-home social, games"', add
	label define main_lbl 74 `"Play musical instrument, sing, act"', add
	label define main_lbl 75 `"Artistic activity"', add
	label define main_lbl 76 `"Crafts"', add
	label define main_lbl 77 `"Hobbies"', add
	label define main_lbl 78 `"Relax, think, do nothing"', add
	label define main_lbl 81 `"Read books"', add
	label define main_lbl 82 `"Read periodicals"', add
	label define main_lbl 83 `"Read newspapers"', add
	label define main_lbl 84 `"Listen to music (cd etc.)"', add
	label define main_lbl 85 `"Listen to radio"', add
	label define main_lbl 86 `"Watch television, video"', add
	label define main_lbl 87 `"Writing by hand"', add
	label define main_lbl 88 `"Conversation, phone, texting"', add
	label define main_lbl 89 `"Use computer"', add
	label define main_lbl 90 `"Imputed travel"', add
	label define main_lbl 91 `"Personal or adult care travel"', add
	label define main_lbl 92 `"Travel as part of paid work"', add
	label define main_lbl 93 `"Travel to/from work + other work travel"', add
	label define main_lbl 94 `"Travel related to education"', add
	label define main_lbl 95 `"Travel related to consumption"', add
	label define main_lbl 96 `"Travel related to child care"', add
	label define main_lbl 97 `"Travel for volunteering or worship"', add
	label define main_lbl 98 `"Other travel"', add
	label define main_lbl -8 `"Item missing"', add
	label values main main_lbl
	
	*****************************************
	*manipulations to classify main activity*
	*****************************************
	*start by generating empty variables for all mutually exclusive categories
	local variable_list other_income job_search paid_work core_home homeowner obtaings otherscare childcare tv socializing esp other_leisure education civic medical other_nec
	
	foreach newvar of local variable_list{
		gen `newvar'_ = .
	}

	*travel does not fit into a single category
	*following Aguiar, we allocate the travel component of each activity into the same variable as the appropriate category
	replace other_income_ = time if main == 12
	replace job_search_ = time if main == 15
	replace paid_work_ = time if inlist(main, 10, 11, 13, 14, 92, 93)
	replace core_home_ = time if inlist(main, 20, 21, 22, 23)
	replace homeowner_ = time if inlist(main, 24, 25)
	replace obtaings_ = time if inlist(main, 26, 27, 28, 30, 31, 32, 95)
	replace otherscare_ = time if inlist(main, 40, 91)
	replace childcare_ = time if inlist(main, 33, 34, 35, 36, 37, 38, 39, 65, 96)
	replace tv_ = time if inlist(main, 86)
	replace socializing_ = time if inlist(main, 71, 72, 73, 57, 88)
	replace esp_ = time if inlist(main, 01, 02, 03, 04, 05, 06, 08, 09, 56)
	replace other_leisure_ = time if inlist(main, 70, 74, 75, 76, 77, 78, 50, 51, 52, 53, 54, 55, 58, 81, 82, 83, 84, 85, 87, 89, 60, 62, 63, 64, 66, 67, 68)
	replace education_ = time if inlist(main, 16, 17, 18, 19, 94)
	replace civic_ = time if inlist(main, 41, 42, 43, 44, 45, 46, 48, 49, 97)
	replace medical_ = time if inlist(main, 07, 29)
	replace other_nec_ = time if inlist(main, 90, 98)

	*generate a sum of the travel and non-travel components for each activity
	foreach variable of local variable_list{
		bysort ident: egen `variable' = sum(`variable'_)
		replace `variable' = `variable'/60*7
	}
	
	drop *_

	*generate remaining major categories
	gen nonmkt_work = core_home + homeowner + obtaings + otherscare
	gen leisure = tv + socializing + esp + other_leisure
	gen other = education + civic + medical + other_nec
	
	*create a leisure category that does not include eating, sleeping, personal care
	gen leisure_noesp = leisure - esp

	
	*get rid of the "activity" records
	drop if rectype == 3
	
	*sanity check: make sure activity categories sum to 168 (24 hours per day times 7 days per week)
	gen test1 = other_income + job_search + paid_work + nonmkt_work + childcare + esp + leisure_noesp + other
	tab test1
	drop test1

	******************************************
	*manipulations to get data at state level*
	******************************************

	*drop any respondent under the age of 18
	drop if age < 18

	*recode demographic variables to match ATUS regressions
	recode sex 2=0 -8=. 9=., gen(male)
	recode ethnic2 1=0 2=1 3=0 -8=. 9=., gen(black)
	recode under18 1/6=1 -8=. 9=., gen(hv_child)

	*recode age to create categories
	recode age 18/27=1 28/200=0 -8=., gen(age1)
	recode age 28/37=1 18/27=0 38/200=0 -8=., gen(age2)
	recode age 38/47=1 18/37=0 48/200=0 -8=., gen(age3)
	recode age 48/57=1 18/47=0 58/200=0 -8=., gen(age4)
	recode age 58/65=1 18/58=0 66/200=0 -8=., gen(age5)

	*recode education to create categories
	recode educ 1/2=1 3/6=0 -8=. 9=., gen(educ1)
	recode educ 3=1 1/2=0 4/6=0 -8=. 9=., gen(educ2)
	recode educ 4=1 1/3=0 5/6=0 -8=. 9=., gen(educ3)
	recode educ 5/6=1 1/4=0 -8=. 9=., gen(educ4)

	*drop unneeded variables
	drop sex ethnic2 under18 age educ

	*to count observations by state
	gen obs = 1
	egen nobs = sum(obs), by(state)

	*if we are looking at 1993, we want to take the average of the 1992-1993 sample
	if "`year'" == "1993"{
	*collapse by state and by year. This gives us separate estimates by year with weights
		collapse (mean) sample male black hv_child age1-age5 educ1-educ4 `variable_list' nonmkt_work leisure leisure_noesp other (sum) nobs [iw=recwght] , by(year state)

	*take an average over the three years
		collapse (mean) sample male black hv_child age1-age5 educ1-educ4 `variable_list' nonmkt_work leisure leisure_noesp other (sum) nobs, by(state)
	}
	
	else {
		collapse (mean) sample male black hv_child age1-age5 educ1-educ4 `variable_list' nonmkt_work leisure leisure_noesp other (sum) nobs [iw=recwght] , by(state)

	}

	*generate a version of the state variable with no labels
	*coding of states doesn't correspond to fips coding
	gen idn = state

	*********************************************
	*export as an excel file
	export delimited "AHTUS_`year'.csv", replace
	save "AHTUS_`year'.dta", replace
}

*****************************
*merge the datasets together*
*****************************

use "AHTUS_1993.dta"
append using "AHTUS_1995.dta"
append using "AHTUS_1998.dta"

erase "AHTUS_1993.dta"
erase "AHTUS_1995.dta"
erase "AHTUS_1998.dta"

cd "..\ATUS_AHTUS_state_datasets"

save "AHTUS_all.dta", replace